{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "527755f1-1296-4e1a-9010-2d5dbf7d0ad3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Load image 0.png success...\n",
      "Load image 00.png success...\n",
      "Output 0-6.png...\n",
      "Output 00-0.png...\n"
     ]
    }
   ],
   "source": [
    "!python3 run.py --input images"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "5bb85955-5a2c-466f-bbf0-90b77052d9f1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "usage: train.py [-h] [--lr LR] [--batch BATCH] [--max-epoch MAX_EPOCH]\n",
      "                [-o OUTPUT] [--log-dir LOG_DIR]\n",
      "\n",
      "Train a model\n",
      "\n",
      "optional arguments:\n",
      "  -h, --help            show this help message and exit\n",
      "  --lr LR               Learning rate\n",
      "  --batch BATCH         Batch size\n",
      "  --max-epoch MAX_EPOCH\n",
      "                        Max training epoch\n",
      "  -o OUTPUT, --output OUTPUT\n",
      "                        The output filename of trained weight and bias\n",
      "  --log-dir LOG_DIR     The log-dir of tensorboard\n"
     ]
    }
   ],
   "source": [
    "!python3 train.py --help"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "acac13ae-39cb-42a3-8e22-0dc6d39a62ea",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The folder ./logs was deleted...\n",
      "成功加载训练集，大小为：60000\n",
      "成功加载测试集，大小为：10000\n",
      "---------第 0 轮训练开始---------\n",
      "训练次数: 100, Loss = 2.312741409478024, Acc = 0.11046875\n",
      "训练次数: 200, Loss = 2.304620104981952, Acc = 0.10953125\n",
      "训练次数: 300, Loss = 2.2993800494781125, Acc = 0.10796875\n",
      "训练次数: 400, Loss = 2.30338536960158, Acc = 0.10546875\n",
      "训练次数: 500, Loss = 2.2891547873876985, Acc = 0.10921875\n",
      "训练次数: 600, Loss = 2.3053359629997727, Acc = 0.11015625\n",
      "训练次数: 700, Loss = 2.2842150061499837, Acc = 0.120625\n",
      "训练次数: 800, Loss = 2.28509753688723, Acc = 0.13078125\n",
      "训练次数: 900, Loss = 2.2682150528991762, Acc = 0.19390625\n",
      "---------第 1 轮训练开始---------\n",
      "训练次数: 1000, Loss = 2.04614864600224, Acc = 0.24466080402010051\n",
      "---测试次数: 1000, Loss = 2.095796257812992, Acc = 0.2233\n",
      "训练次数: 1100, Loss = 1.9231465004010244, Acc = 0.2525\n",
      "训练次数: 1200, Loss = 1.825788324287775, Acc = 0.3334375\n",
      "训练次数: 1300, Loss = 1.6431771272343823, Acc = 0.43828125\n",
      "训练次数: 1400, Loss = 1.3054988164795318, Acc = 0.565625\n",
      "训练次数: 1500, Loss = 1.2656990437924054, Acc = 0.6534375\n",
      "训练次数: 1600, Loss = 1.0453949993820126, Acc = 0.7046875\n",
      "训练次数: 1700, Loss = 0.9724207123605905, Acc = 0.7775\n",
      "训练次数: 1800, Loss = 0.9171621440938762, Acc = 0.828125\n",
      "---------第 2 轮训练开始---------\n",
      "训练次数: 1900, Loss = 0.7168979582357556, Acc = 0.8509736180904522\n",
      "训练次数: 2000, Loss = 0.5006390393290641, Acc = 0.8775\n",
      "---测试次数: 2000, Loss = 0.558431435462517, Acc = 0.8856\n",
      "训练次数: 2100, Loss = 0.6642685998968161, Acc = 0.89859375\n",
      "训练次数: 2200, Loss = 0.3929371151063725, Acc = 0.9403125\n",
      "训练次数: 2300, Loss = 0.3304526834029483, Acc = 0.94640625\n",
      "训练次数: 2400, Loss = 0.37612605967835433, Acc = 0.95375\n",
      "训练次数: 2500, Loss = 0.31304488662252217, Acc = 0.95390625\n",
      "训练次数: 2600, Loss = 0.21045986348887272, Acc = 0.95609375\n",
      "训练次数: 2700, Loss = 0.2664004486866358, Acc = 0.96296875\n",
      "训练次数: 2800, Loss = 0.23169276561019153, Acc = 0.96\n",
      "---------第 3 轮训练开始---------\n",
      "训练次数: 2900, Loss = 0.1831847114034338, Acc = 0.9711055276381909\n",
      "训练次数: 3000, Loss = 0.19483955187754476, Acc = 0.9678125\n",
      "---测试次数: 3000, Loss = 0.18556359082482182, Acc = 0.9702\n",
      "训练次数: 3100, Loss = 0.10931560489847882, Acc = 0.9684375\n",
      "训练次数: 3200, Loss = 0.14247233737576465, Acc = 0.97125\n",
      "训练次数: 3300, Loss = 0.14936308748010219, Acc = 0.97171875\n",
      "训练次数: 3400, Loss = 0.16912917495459912, Acc = 0.971875\n",
      "训练次数: 3500, Loss = 0.17684996576095036, Acc = 0.97390625\n",
      "训练次数: 3600, Loss = 0.2702456807655461, Acc = 0.975\n",
      "训练次数: 3700, Loss = 0.19366366821306036, Acc = 0.976875\n",
      "---------第 4 轮训练开始---------\n",
      "训练次数: 3800, Loss = 0.08165184678982756, Acc = 0.9775439698492462\n",
      "训练次数: 3900, Loss = 0.09178620020023844, Acc = 0.975\n",
      "训练次数: 4000, Loss = 0.07349540337962913, Acc = 0.975\n",
      "---测试次数: 4000, Loss = 0.11999721732665546, Acc = 0.9767\n",
      "训练次数: 4100, Loss = 0.1997567820577883, Acc = 0.9775\n",
      "训练次数: 4200, Loss = 0.09725383061800613, Acc = 0.9759375\n",
      "训练次数: 4300, Loss = 0.16079673454381818, Acc = 0.980625\n",
      "训练次数: 4400, Loss = 0.10330402673636124, Acc = 0.975\n",
      "训练次数: 4500, Loss = 0.09041423786774384, Acc = 0.9775\n",
      "训练次数: 4600, Loss = 0.0706682354195287, Acc = 0.9765625\n",
      "---------第 5 轮训练开始---------\n",
      "训练次数: 4700, Loss = 0.08096374219118245, Acc = 0.9792713567839196\n",
      "训练次数: 4800, Loss = 0.07451231187061205, Acc = 0.9809375\n",
      "训练次数: 4900, Loss = 0.05419459860769288, Acc = 0.98046875\n",
      "训练次数: 5000, Loss = 0.061923618184799786, Acc = 0.97875\n",
      "---测试次数: 5000, Loss = 0.08985135453409342, Acc = 0.9814\n",
      "训练次数: 5100, Loss = 0.04824237612182413, Acc = 0.981875\n",
      "训练次数: 5200, Loss = 0.04331134164843774, Acc = 0.9796875\n",
      "训练次数: 5300, Loss = 0.05256231949045398, Acc = 0.98296875\n",
      "训练次数: 5400, Loss = 0.050919782758230586, Acc = 0.9790625\n",
      "训练次数: 5500, Loss = 0.135183069189341, Acc = 0.98109375\n",
      "训练次数: 5600, Loss = 0.04226560816123742, Acc = 0.9821875\n",
      "---------第 6 轮训练开始---------\n",
      "训练次数: 5700, Loss = 0.07649516573225344, Acc = 0.9831972361809045\n",
      "训练次数: 5800, Loss = 0.11258321197461339, Acc = 0.98421875\n",
      "训练次数: 5900, Loss = 0.07102528126678012, Acc = 0.98234375\n",
      "训练次数: 6000, Loss = 0.12661239491084603, Acc = 0.980625\n",
      "---测试次数: 6000, Loss = 0.08467001101313758, Acc = 0.9809\n",
      "训练次数: 6100, Loss = 0.04329003211081554, Acc = 0.98234375\n",
      "训练次数: 6200, Loss = 0.12263332088001713, Acc = 0.98078125\n",
      "训练次数: 6300, Loss = 0.07337894609765676, Acc = 0.9825\n",
      "训练次数: 6400, Loss = 0.07259068686447087, Acc = 0.983125\n",
      "训练次数: 6500, Loss = 0.05466150656773166, Acc = 0.980625\n",
      "---------第 7 轮训练开始---------\n",
      "训练次数: 6600, Loss = 0.042915996319072496, Acc = 0.9795854271356784\n",
      "训练次数: 6700, Loss = 0.0244556346886787, Acc = 0.98375\n",
      "训练次数: 6800, Loss = 0.05697181974449256, Acc = 0.98578125\n",
      "训练次数: 6900, Loss = 0.030065878205953304, Acc = 0.9853125\n",
      "训练次数: 7000, Loss = 0.10316362085870295, Acc = 0.98328125\n",
      "---测试次数: 7000, Loss = 0.08121863812551346, Acc = 0.9792\n",
      "训练次数: 7100, Loss = 0.17812926313888824, Acc = 0.98359375\n",
      "训练次数: 7200, Loss = 0.08835864630912166, Acc = 0.9809375\n",
      "训练次数: 7300, Loss = 0.12809350535805114, Acc = 0.9834375\n",
      "训练次数: 7400, Loss = 0.023236043762771177, Acc = 0.9859375\n",
      "训练次数: 7500, Loss = 0.1381081900031174, Acc = 0.98109375\n",
      "---------第 8 轮训练开始---------\n",
      "训练次数: 7600, Loss = 0.049399310458425195, Acc = 0.9864949748743719\n",
      "训练次数: 7700, Loss = 0.03289651614138743, Acc = 0.98546875\n",
      "训练次数: 7800, Loss = 0.12358326554789244, Acc = 0.97921875\n",
      "训练次数: 7900, Loss = 0.04094663797318691, Acc = 0.9875\n",
      "训练次数: 8000, Loss = 0.06247137725322409, Acc = 0.98375\n",
      "---测试次数: 8000, Loss = 0.07145064499182843, Acc = 0.9823\n",
      "训练次数: 8100, Loss = 0.041307668584471324, Acc = 0.98109375\n",
      "训练次数: 8200, Loss = 0.021641939055418842, Acc = 0.98234375\n",
      "/home/xbx/mnist-numpy/Layer.py:119: RuntimeWarning: overflow encountered in exp\n",
      "  self.x = 1 / (1 + np.exp(-x))\n",
      "训练次数: 8300, Loss = 0.0518288904323982, Acc = 0.9828125\n",
      "训练次数: 8400, Loss = 0.038615982220989886, Acc = 0.98078125\n",
      "---------第 9 轮训练开始---------\n",
      "训练次数: 8500, Loss = 0.03502704303478794, Acc = 0.9814698492462312\n",
      "训练次数: 8600, Loss = 0.05156901901164241, Acc = 0.9828125\n",
      "训练次数: 8700, Loss = 0.028654202840295614, Acc = 0.98453125\n",
      "训练次数: 8800, Loss = 0.024565432994319486, Acc = 0.9853125\n",
      "训练次数: 8900, Loss = 0.08921401134257367, Acc = 0.98515625\n",
      "训练次数: 9000, Loss = 0.06074979041200612, Acc = 0.98296875\n",
      "---测试次数: 9000, Loss = 0.08158534468386805, Acc = 0.9795\n",
      "训练次数: 9100, Loss = 0.02833933406163923, Acc = 0.984375\n",
      "训练次数: 9200, Loss = 0.12113929619293323, Acc = 0.984375\n",
      "训练次数: 9300, Loss = 0.10125834364602297, Acc = 0.98421875\n",
      "---------第 10 轮训练开始---------\n",
      "训练次数: 9400, Loss = 0.04769835116805285, Acc = 0.9852386934673367\n",
      "训练次数: 9500, Loss = 0.036482982985027924, Acc = 0.9803125\n",
      "训练次数: 9600, Loss = 0.051427679652581604, Acc = 0.9846875\n",
      "训练次数: 9700, Loss = 0.022655253884780565, Acc = 0.983125\n",
      "训练次数: 9800, Loss = 0.03592151757887935, Acc = 0.9828125\n",
      "训练次数: 9900, Loss = 0.07016586132610234, Acc = 0.9865625\n",
      "训练次数: 10000, Loss = 0.13194273342132065, Acc = 0.98328125\n",
      "---测试次数: 10000, Loss = 0.06744877296030387, Acc = 0.9823\n",
      "训练次数: 10100, Loss = 0.0924209510106594, Acc = 0.98453125\n",
      "训练次数: 10200, Loss = 0.06281651045993777, Acc = 0.9871875\n",
      "训练次数: 10300, Loss = 0.07003547351396644, Acc = 0.98359375\n",
      "---------第 11 轮训练开始---------\n",
      "训练次数: 10400, Loss = 0.03807271424686795, Acc = 0.9863379396984925\n",
      "训练次数: 10500, Loss = 0.0876320511599915, Acc = 0.98234375\n",
      "训练次数: 10600, Loss = 0.02800794843061876, Acc = 0.9825\n",
      "训练次数: 10700, Loss = 0.02861183747628815, Acc = 0.985625\n",
      "训练次数: 10800, Loss = 0.020626145608915934, Acc = 0.98109375\n",
      "训练次数: 10900, Loss = 0.1178926948621865, Acc = 0.9828125\n",
      "训练次数: 11000, Loss = 0.029864289592453765, Acc = 0.98640625\n",
      "---测试次数: 11000, Loss = 0.06941152665893575, Acc = 0.9821\n",
      "训练次数: 11100, Loss = 0.03989123095018825, Acc = 0.98546875\n",
      "训练次数: 11200, Loss = 0.04942260440339607, Acc = 0.9825\n"
     ]
    }
   ],
   "source": [
    "!python3 train.py --lr 0.036 --batch 64 --log-dir ./logs --max-epoch 12"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "134353c2-eb57-425e-8d9b-69b118cdfe9a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "49188cb2b7e84264b22c201c57a48504",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "interactive(children=(Dropdown(description='file', options=('0.png', '00.png'), value='0.png'), Output()), _do…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import ipywidgets as widgets\n",
    "from ipywidgets import interact, interact_manual\n",
    "import os\n",
    "from IPython.display import Image\n",
    "fdir = 'images/'\n",
    "@interact\n",
    "def show_images(file=os.listdir(fdir)):\n",
    "    display(Image(fdir+file))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "775ee25c-1303-40fc-9a80-903442c61143",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "95db08defccc4383a376c35b83699251",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "interactive(children=(Dropdown(description='file', options=('0-6.png', '00-4.png'), value='0-6.png'), Output()…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fdir = 'run/'\n",
    "@interact\n",
    "def show_images(file=os.listdir(fdir)):\n",
    "    display(Image(fdir+file))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "6830b072-9dc0-4264-b13c-16608bd12858",
   "metadata": {},
   "outputs": [],
   "source": [
    "!rm ./run/*.png"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "677fcd76-2ff3-4596-85e8-2e15d3b987cc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The tensorboard extension is already loaded. To reload it, use:\n",
      "  %reload_ext tensorboard\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "\n",
       "      <iframe id=\"tensorboard-frame-27c5462a6d81d37f\" width=\"100%\" height=\"800\" frameborder=\"0\">\n",
       "      </iframe>\n",
       "      <script>\n",
       "        (function() {\n",
       "          const frame = document.getElementById(\"tensorboard-frame-27c5462a6d81d37f\");\n",
       "          const url = new URL(\"/\", window.location);\n",
       "          const port = 6006;\n",
       "          if (port) {\n",
       "            url.port = port;\n",
       "          }\n",
       "          frame.src = url;\n",
       "        })();\n",
       "      </script>\n",
       "    "
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%load_ext tensorboard\n",
    "%tensorboard --logdir ./logs --bind_all"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aad76833-8d80-46ed-8f1f-af97421957ee",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
